﻿#region 命名空间

using System;
using System.Collections.Generic;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Templates.WinnerCMS6.Search
{
    public partial class Index : PublicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(KeyWord))
                {
                    this.LabelSummary.Text = "请输入关键字";
                }

                Title = string.Concat(KeyWord, " -- ", WebConfig.SiteName);
            }
        }

        private void Bind()
        {
            if (string.IsNullOrEmpty(KeyWord))
            {
                return;
            }
            var pp = new PagingParam
            {
                PageNo = this.PageNavigation1.PageNo,
                PageSize = this.PageNavigation1.PageSize
            };
            var list = new CoreManage().Search("ContentTitle", KeyWord, pp);
            var outlist = new List<dynamic>();
            foreach (var t in list)
            {
                var url = StaticFilePath.ContentShowUrl(t);
                outlist.Add(new
                {
                    ID = t.Id,
                    t.ContentTitle,
                    t.UpdateTime,
                    Content = t.Brief,
                    Link = url
                });
            }
            this.Repeater1.DataSource = outlist;
            this.Repeater1.DataBind();

            //LabelSummary.Text = this.search.Summary;

            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        /// <summary>
        ///     绑定 Lucene.net 搜索结果
        /// </summary>
        private void BindLucene()
        {
            if (string.IsNullOrEmpty(KeyWord))
            {
                return;
            }

            var search = new PlugIn.Indexer.Search
            {
                PagingParam = new PagingParam
                {
                    PageNo = this.PageNavigation1.PageNo,
                    PageSize = this.PageNavigation1.PageSize
                }
            };

            this.Repeater1.DataSource = search.GetData();
            this.Repeater1.DataBind();

            this.LabelSummary.Text = search.Summary;

            this.PageNavigation1.RecordCount = search.PagingParam.RecordCount;
        }


        private string KeyWord
        {
            get { return Request.QueryString["K"]; }
        }

        protected void PageNavigation1_PageChanged(object sender, EventArgs e)
        {
            //if (!CMSConfig.Instance().EnableLucene)
            //{
            //    Bind();
            //}
            //else
            {
                BindLucene();
            }
        }
    }
}